<?php
/*
 * 医院模型
 * 用来定义与医院有关的数据库操作
 *
 */
class Hospital_model extends Model{

    //构造函数
    function Hospital_model(){
        parent::Model();
        $this->load->database();  //加载数据库
    }

//---------------------------------------------------医院部分----------------------------------------------------//

	//获取医院级别信息(有ID则输出该ID对应信息，无ID则输出全部信息)
	function get_hsrank_info($id=null) {
		if($id!=null) {
			$this->db->where('Id',$id);
			$query=$this->db->get('dc_hsrank');
			$result=$query->row_array();
            $result=$result['rankname'];
		}else{
			$query=$this->db->get('dc_hsrank');
			$result= $query->result_array();
		}
		return $result;
	}

	//获取医院名称列表
	function get_hospital_name($areacode='ALL'){
		$this->db->select('hs_id, name');
		if($areacode!='ALL') {//如果指定了地区
			$this->db->where('areacode',$areacode);
		}
        $query=$this->db->get('dc_hospital');
		$result= $query->result_array();
		return $result;
	}

	//获取医院列表
	function get_hospital_list($areacode='ALL',$native_id=0,$per_nums=null,$start_position=null) {
		if(($per_nums!=null )||($start_position!=null)){
			$this->db->limit($per_nums,$start_position);
		}
		if($areacode!='ALL') {//如果指定了地区
			$this->db->where('areacode',$areacode);
		}
		if ($native_id!=0){
			$this->db->where('native_id',$native_id);
		}
		$this->db->order_by('index_id','desc');//按index_id倒序排列
        $query=$this->db->get('dc_hospital');
		$result= $query->result_array();
		$c=count($result)-1;
		for($i=0;$i<=$c;$i++) {
			$rankname=$this->get_hsrank_info($result[$i]['rank']);
			$result[$i]['rank']=$rankname;
		}
		return $result;
	}

	//获取评价最高的前8名医院
	function get_hot_hospital($areacode='ALL',$num=8) {
		$this->db->limit($num);
		if($areacode!='ALL') {//如果指定了地区
			$this->db->where('areacode',$areacode);
		}
		$this->db->select('Id,areacode,hs_id,name,com_num');
		$this->db->order_by('com_num','desc');//按index_id倒序排列
		$query=$this->db->get('dc_hospital');
		$result= $query->result_array();
		return $result;
		

	}

	//获取医院数量
	function get_hospital_num($areacode='ALL',$native_id=0) {
		if ($native_id==0){
			if($areacode!='ALL') {
				$query_string="select count(*) as c from dc_hospital where areacode='".$areacode."'";
			}else{
				$query_string="select count(*) as c from dc_hospital";
			}
		}else{
			if($areacode!='ALL') {
				$query_string="select count(*) as c from dc_hospital where areacode='".$areacode."' and native_id='".$native_id."'";
			}else{
				$query_string="select count(*) as c from dc_hospital where native_id='".$native_id."'";
			}
		}
		$query = $this->db->query($query_string);
		$result= $query->result_array();
		return $result[0]['c'];
	}

	//新增医院
	function add_hospital($hs) {
		$data = array(
            'areacode' => $hs['areacode'] ,
            'name' => $hs['name'] ,
			'image' => $hs['image'] ,
			'rank' => $hs['rank'] ,
			'bednum' => $hs['bednum'] ,
			'website' => $hs['website'] ,
			'brief' => $hs['brief'] ,
			'intro' => $hs['intro'] ,
			'vip' => $hs['vip'] ,
			'address' => $hs['address'] ,
			'contact' => $hs['contact'] ,
			'busline' => $hs['busline'] ,
			'puttime' => $hs['puttime'] );
		 $this->db->insert('dc_hospital', $data);    //插入数据
		 if(($this->db->affected_rows())!=1){
            return 1;      //如果插入操作影响的行数不为1，则表示插入失败，返回1
        }else{
			$insertid=$this->db->insert_id();
            return $insertid ;    //如果插入成功，返回插入ID
        }
	}

	//生成以区码开头的医院ID,并插入医院数据中
	function add_hs_id($areacode,$id) {
		$hs_id=$areacode.str_pad($id,3,"0",STR_PAD_LEFT);//不够三位的补全
		$this->db->where('Id',$id);
		$data=array('hs_id'=>$hs_id);
		$query=$this->db->update('dc_hospital',$data); 
		if($this->db->affected_rows()==1){
            return 0;//更新成功，返回0
        }else{
            return 1;//失败，返回1
        }
	}

	//根据HSID获取医院信息
	function get_onehospital_info($hs_id) {
		$this->db->where('hs_id',$hs_id);
		$query=$this->db->get('dc_hospital');
		$result=$query->row_array();
		return $result;
	}

	//根据HSID获取医院名称
	function get_onehospital_name($hs_id) {
		$this->db->where('hs_id',$hs_id);
		$this->db->select('name');
		$query=$this->db->get('dc_hospital');
		$result=$query->row_array();
		if(!empty($result)) {
			return $result['name'];
		}else{
			return '';
		}
	}

	//编辑医院
	function edit_hospital($hs_id,$hs) {
		$data = array(
            'name' => $hs['name'] ,
			'rank' => $hs['rank'] ,
			'bednum' => $hs['bednum'] ,
			'website' => $hs['website'] ,
			'brief' => $hs['brief'] ,
			'intro' => $hs['intro'] ,
			'vip' => $hs['vip'] ,
			'address' => $hs['address'] ,
			'contact' => $hs['contact'] ,
			'busline' => $hs['busline'] ,
			'puttime' => $hs['puttime'] );
		if ($hs['image'] != '') {//如果不为空，则更新图片字段
			$data['image'] = $hs['image'];
			}
		 $this->db->where('hs_id',$hs_id);
		 $query=$this->db->update('dc_hospital',$data); 
		 if(($this->db->affected_rows())!=1){
            return 1;      //如果更新操作影响的行数不为1，则表示插入失败，返回1
        }else{
            return 0 ;    //成功，返回0
        }
	}


	//删除医院
	function del_hospital($hs_id) {
		$this->db->where("hs_id",$hs_id); 
        $query=$this->db->delete('dc_hospital');
	}


	//更新信息表中的数量
	function edit_num($tab,$id,$col,$num) {
		$this->db->set($col, $col.$num, FALSE);
		if($tab=='dc_hospital') { //如果更新的是医院信息表，则查询HSID
			$this->db->where('hs_id',$id);
		}else{
			$this->db->where('Id',$id);
		}
		$query=$this->db->update($tab);
		if(($this->db->affected_rows())!=1){
            return 1;      //如果更新操作影响的行数不为1，则表示插入失败，返回1
        }else{
            return 0 ;    //成功，返回0
        }
		
	}

	//获取医院所在地区行政区域
	function get_nativearea_list($areacode) {
		$this->db->where('areacode',$areacode);
		$query=$this->db->get('dc_nativearea');
		$result= $query->result_array();
		return $result;
	}

	//获取行政区域名称
	function get_nativearea_name($native_id) {
		$this->db->where('Id',$native_id);
		$query=$this->db->get('dc_nativearea');
		$result= $query->row_array();
		return $result;
	}

//---------------------------------------科室部分--------------------------------------//

	//获取科室分类(有ID则输出该ID对应信息，无ID则输出全部信息)
	function get_depttype_info($id=null) {
		if($id!=null) {
			$this->db->where('Id',$id);
			$query=$this->db->get('dc_depttype');
			$result=$query->row_array();
            $result=$result['typename'];
		}else{
			$query=$this->db->get('dc_depttype');
			$result= $query->result_array();
		}
		return $result;
	}

	//新增科室
	function add_dept($dp) {
		$data = array(
            'areacode' => $dp['areacode'] ,
			'hs_id' => $dp['hs_id'] ,
            'name' => $dp['name'] ,
			'image' => $dp['image'] ,
			'depttype' => $dp['depttype'] ,
			'intro' => $dp['intro'] ,
			'tel' => $dp['tel'] ,
			'puttime' => $dp['puttime'] );
		 $this->db->insert('dc_dept', $data);    //插入数据
		 if($this->db->affected_rows()!=1){
            return 1;   //如果插入操作影响的行数不为1，则表示插入失败，返回1
         }else{
            return 0; //如果插入成功，返回0
        }
	}


	//获取科室数量
	function get_dept_num($hs_id=null,$areacode='ALL',$type_id=0) {
		if ($type_id==0){ //如果没有指定科室类别
			if($hs_id!=null && $areacode=='ALL') { //只指定了HSID
				$query_string="select count(*) as c from dc_dept where hs_id='".$hs_id."'";
			}elseif($hs_id==null && $areacode!='ALL'){ //只指定了areacode
				$query_string="select count(*) as c from dc_dept where areacode='".$areacode."'";
			}elseif($hs_id==null && $areacode=='ALL'){ //没指定参数
				$query_string="select count(*) as c from dc_dept";
			}
		}else{
			if($hs_id==null && $areacode!='ALL'){ //只指定了areacode
				$query_string="select count(*) as c from dc_dept where areacode='".$areacode."' and depttype='".$type_id."'";
			}elseif($hs_id==null && $areacode=='ALL'){ //没指定参数
				$query_string="select count(*) as c from dc_dept where depttype='".$type_id."'";
			}
		}
		$query = $this->db->query($query_string);
		$result= $query->result_array();
		return $result[0]['c'];
	}

	//获取科室列表
	function get_dept_list($hs_id=null,$areacode='ALL',$per_nums=null,$start_position=null,$type_id=0) {
		if(($per_nums!=null )||($start_position!=null)){
			$this->db->limit($per_nums,$start_position);
		}
		if($hs_id!=null) {//如果指定了HSID
			$this->db->where('hs_id',$hs_id);
		}
		if($areacode!='ALL') {//如果指定了地区
			$this->db->where('areacode',$areacode);
		}
		if($type_id!=0) {//如果指定了科室类别
			$this->db->where('depttype',$type_id);
		}
		$this->db->order_by('depttype','asc');//按科室类别排序
        $query=$this->db->get('dc_dept');
		$result= $query->result_array();
		if(!empty($result)) {
			$c=count($result)-1;
			for($i=0;$i<=$c;$i++) {
				$typename=$this->get_depttype_info($result[$i]['depttype']);
				$result[$i]['depttype']=$typename;
			}
		return $result;
		}
			
	}

	//获取科室名称列表
	function get_dept_name($areacode='ALL'){
		$this->db->select('Id,hs_id,name');
		if($areacode!='ALL') {//如果指定了地区
			$this->db->where('areacode',$areacode);
		}
        $query=$this->db->get('dc_dept');
		$result= $query->result_array();
		return $result;
	}

	//根据id获取科室信息
	function get_onedept_info($id) {
		$this->db->where('Id',$id);
		$query=$this->db->get('dc_dept');
		$result=$query->row_array();
		return $result;
	}

	//编辑科室
	function edit_dept($id,$dp) {
		$data = array(
            'name' => $dp['name'] ,
			'depttype' => $dp['depttype'] ,
			'intro' => $dp['intro'] ,
			'tel' => $dp['tel'] ,
			'puttime' => $dp['puttime'] );
		if ($dp['image'] != '') {//如果不为空，则更新图片字段
			$data['image'] = $dp['image'];
			}
		$this->db->where('Id',$id);
		$this->db->update('dc_dept', $data);    //插入数据
		if($this->db->affected_rows()!=1){
            return 1;   //如果更新操作影响的行数不为1，则表示插入失败，返回1
        }else{
            return 0; //如果更新成功，返回0
        }
	}

	//根据id删除科室
	function del_dept($id) {
		$this->db->where("Id",$id); 
        $query=$this->db->delete('dc_dept');
	}

	//根据医院HSID删除科室
	function del_hos_dept($hs_id) {
		$this->db->where("hs_id",$hs_id);
        $query=$this->db->delete('dc_dept');
	}


	//获取所有科室姓名列表(group_by)
	function get_deptname_list($areacode='ALL') {
		if($areacode!='ALL') {
			$this->db->where("areacode",$areacode);
		}
		$this->db->group_by("name"); 
		$this->db->select('hs_id, depttype, name, doct_num');
		$query=$this->db->get('dc_dept');
		$result= $query->result_array();
		return $result;
	}


//--------------------------------------------医生部分--------------------------------------------------//

	//获取医生级别信息(有ID则输出该ID对应信息，无ID则输出全部信息)
	function get_dcrank_info($id=null) {
		if($id!=null) {
			$this->db->where('Id',$id);
			$query=$this->db->get('dc_dcrank');
			$result=$query->row_array();
            $result=$result['rankname'];
		}else{
			$this->db->order_by("Id", "desc");
			$query=$this->db->get('dc_dcrank');
			$result= $query->result_array();
		}
		return $result;
	}

	//新增医生
	function add_doctor($dc) {
		$data = array(
            'areacode' => $dc['areacode'] ,
			'hs_id' => $dc['hs_id'] ,
            'dp_id' => $dc['dp_id'] ,
			'name' => $dc['name'] ,
			'sex' => $dc['sex'] ,
			'rank' => $dc['rank'] ,
			'special' => $dc['special'] ,
			'intro' => $dc['intro'] ,
			'worktime' => $dc['worktime'] ,
			'puttime' => $dc['puttime'] );
		 $this->db->insert('dc_doctor', $data);    //插入数据
		 if($this->db->affected_rows()!=1){
            return 1;   //如果插入操作影响的行数不为1，则表示插入失败，返回1
         }else{
            return 0; //如果插入成功，返回0
        }
	}


	//获取医生列表(根据HSID或科室ID获取)
	function get_doctor_list($col,$id,$per_nums=null,$start_position=null) {
		if(($per_nums!=null )||($start_position!=null)){
			$this->db->limit($per_nums,$start_position);
		}
		if($col=='hs_id') {
			$this->db->order_by("dp_id", "desc");//按科室倒序排列
			$this->db->where('hs_id',$id);//根据HSID获取
		}
		if($col=='dp_id') {
			$this->db->order_by("Id", "desc");//按ID倒序排列
			$this->db->where('dp_id',$id);//根据科室ID获取
		}
        $query=$this->db->get('dc_doctor');
		$result= $query->result_array();
		return $result;
	}

	//获取医生数量（根据HSID或科室ID获取）
	function get_doctor_num($col,$id) {
		$query_string="select count(*) as c from dc_doctor where ".$col."='".$id."'";
		$query = $this->db->query($query_string);
		$result= $query->result_array();
		return $result[0]['c'];
	}

	//根据id获取医生信息
	function get_onedoctor_info($id) {
		$this->db->where('Id',$id);
		$query=$this->db->get('dc_doctor');
		$result=$query->row_array();
		return $result;
	}

	//编辑医生
	function edit_doctor($id,$dc) {
		$data = array(
            'dp_id' => $dc['dp_id'] ,
			'name' => $dc['name'] ,
			'sex' => $dc['sex'] ,
			'rank' => $dc['rank'] ,
			'special' => $dc['special'] ,
			'intro' => $dc['intro'] ,
			'worktime' => $dc['worktime'] ,
			'puttime' => $dc['puttime'] );
		$this->db->where('Id',$id);
		$this->db->update('dc_doctor', $data);    //插入数据
		if($this->db->affected_rows()!=1){
            return 1;   //如果更新操作影响的行数不为1，则表示插入失败，返回1
        }else{
            return 0; //如果更新成功，返回0
        }
	}

	//根据id删除医生
	function del_doctor($id) {
		$this->db->where("Id",$id); 
        $query=$this->db->delete('dc_doctor');
	}

	//根据医院HSID删除医生
	function del_hos_doct($hs_id) {
		$this->db->where("hs_id",$hs_id);
        $query=$this->db->delete('dc_doctor');
	}

	//根据科室ID删除医生
	function del_dept_doct($dp_id) {
		$this->db->where("dp_id",$dp_id); 
        $query=$this->db->delete('dc_doctor');
	}

//--------------------------------------体检中心部分-----------------------------------------//

	//新增体检中心
	function add_exam($ex) {
		$data = array(
            'areacode' => $ex['areacode'] ,
			'hs_id' => $ex['hs_id'] ,
			'name' => $ex['name'] ,
			'image' => $ex['image'] ,
			'intro' => $ex['intro'] ,
			'vip' => $ex['vip'] ,
			'contact' => $ex['contact'] ,
			'puttime' => $ex['puttime'] );
		 $this->db->insert('dc_exam', $data);    //插入数据
		 if($this->db->affected_rows()!=1){
            return 1;   //如果插入操作影响的行数不为1，则表示插入失败，返回1
         }else{
            return 0; //如果插入成功，返回0
        }
	}

	//获取体检中心列表
	function get_exam_list($areacode='ALL',$per_nums=null,$start_position=null) {
		if(($per_nums!=null )||($start_position!=null)){
			$this->db->limit($per_nums,$start_position);
		}
		if($areacode!='ALL') {//如果指定了地区
			$this->db->where('areacode',$areacode);
		}
		$this->db->order_by('Id','random');//按Id随机排列
        $query=$this->db->get('dc_exam');
		$result= $query->result_array();
		return $result;
	}

	//获取体检中心数量
	function get_exam_num($areacode='ALL') {
		if($areacode!='ALL') {
			$query_string="select count(*) as c from dc_exam where areacode='".$areacode."'";
		}else{
			$query_string="select count(*) as c from dc_exam";
		}
		$query = $this->db->query($query_string);
		$result= $query->result_array();
		return $result[0]['c'];
	}

	//根据id获取体检中心信息
	function get_oneexam_info($id) {
		$this->db->where('Id',$id);
		$query=$this->db->get('dc_exam');
		$result=$query->row_array();
		return $result;
	}

	//编辑体检中心
	function edit_exam($id,$ex) {
		$data = array(
			'hs_id' => $ex['hs_id'] ,
			'name' => $ex['name'] ,
			'intro' => $ex['intro'] ,
			'vip' => $ex['vip'] ,
			'contact' => $ex['contact'] ,
			'puttime' => $ex['puttime'] );
		if ($ex['image'] != '') {//如果不为空，则更新图片字段
			$data['image'] = $ex['image'];
			}
		$this->db->where('Id',$id);
		$this->db->update('dc_exam', $data);    //插入数据
		if($this->db->affected_rows()!=1){
            return 1;   //如果更新操作影响的行数不为1，则表示插入失败，返回1
        }else{
            return 0; //如果更新成功，返回0
        }
	}

	//根据id删除体检中心
	function del_exam($id) {
		$this->db->where("Id",$id); 
        $query=$this->db->delete('dc_exam');
	}

}
?>